/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.excesys.exsecs.om.dto.EquipmentModelDto;
import com.excesys.exsecs.om.entity.OmEquipmentModelMaintenance;
import com.excesys.exsecs.om.mapper.OmEquipmentMapper;
import com.excesys.exsecs.om.mapper.OmMaintenanceItemsMapper;
import com.excesys.exsecs.om.service.OmEquipmentModelService;
import com.excesys.exsecs.om.vo.EquipmentModelExcelVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class OmEquipmentServiceImpl extends ServiceImpl<OmEquipmentMapper, OmEquipmentModelMaintenance>
		implements OmEquipmentModelService {

	private final OmMaintenanceItemsMapper omMaintenanceItemsMapper;

	private final OmEquipmentMapper omEquipmentMapper;

	@Override
	public boolean saveOmEquimentModel(EquipmentModelDto equipmentModelDto) {
		OmEquipmentModelMaintenance omEquipmentModelMaintenance = new OmEquipmentModelMaintenance();
		BeanUtils.copyProperties(equipmentModelDto, omEquipmentModelMaintenance);
		SqlHelper.retBool(this.getBaseMapper().insert(omEquipmentModelMaintenance));
		Optional.ofNullable(equipmentModelDto.getItemsList()).ifPresent(itemsList -> {
			itemsList.stream().map(omMaintenanceItems -> {
				omMaintenanceItems.setEquipmentModelId(omEquipmentModelMaintenance.getId().toString());
				return omMaintenanceItems;
			}).forEach(omMaintenanceItemsMapper::insert);
		});
		return true;
	}

	@Override
	public IPage<EquipmentModelDto> getOmEquipMentModel(Page page,
			OmEquipmentModelMaintenance equipmentModelMaintenance) {
		return baseMapper.getEquipmnetModelPage(page, equipmentModelMaintenance);
	}

	@Override
	public List<EquipmentModelExcelVo> listBamSBuildings() {
		List<OmEquipmentModelMaintenance> bamSBuildings = this.list(Wrappers.emptyWrapper());
		// 转换成execl 对象输出
		return bamSBuildings.stream().map(buildings -> {
			EquipmentModelExcelVo equipmentModelExcelVo = new EquipmentModelExcelVo();
			BeanUtil.copyProperties(buildings, equipmentModelExcelVo);
			return equipmentModelExcelVo;
		}).collect(Collectors.toList());
	}

	@Override
	public List<OmEquipmentModelMaintenance> getListByModelCode(String modelCode) {
		return omEquipmentMapper.getListByModelCode(modelCode);
	}

}
